package com.jboom.jboomconverter.factory;


/**
 * @author zhangwen
 * @date 2023/5/27 13:58
 */
public class ConverterFactory {

    /**
     * 获取代理类
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getConverter(Class<T> clazz) {
        if (!clazz.isInterface()) {
            throw new RuntimeException("only support interface");
        }
        // 因为代码是生成在同包的impl包下，名称多了Impl，直接加载即可
        String packageName = clazz.getPackageName();
        String simpleName = clazz.getSimpleName();
        String implPackageName = packageName + ".impl";
        String implName = simpleName + "Impl";
        try {
            Class<?> implClass = Class.forName(implPackageName + "." + implName);
            return (T) implClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("load mapper error", e);
        }
    }
}
